import * as THREE from 'three'
import { lon2xyz } from './Basic'
import map_wl from '@/assets/images/earth/label-old.png'

export let infoMarkTexTure = null
export const addPointMark = (item, threeInfo) => {
  let { radius, group, currentPath, scene } = threeInfo
  // 根据 perData 计算大小
  let coefficient = 0
  const quCode = ['0000111072', '0000111073', '0000110150', '0000110144'] // 区域代码

  if (!quCode.includes(item.scope_code) || currentPath === '/human') {
    let sLevel = item.performance_proportion_level
    let min = 1.5
    let max = 7
    coefficient = (sLevel - 1) * ((max - min) / 9) + min
  }
  let x = coefficient
  let y = coefficient

  // 人力驾驶舱添加中国区光圈并屏蔽其他所有管家光圈
  if (currentPath === '/human') {
    quCode.push('0000111104') // 添加中国区
    if (!quCode.includes(item.scope_code) && currentPath === '/human') {
      x = 0
      y = 0
    }
  }

  // if (isLogin.value === false) {
  //   x = 1.5
  //   y = 1.5
  // }
  let cityGeometry = new THREE.PlaneBufferGeometry(x, y) //默认在XOY平面上 光圈大小
  let textureLoader = new THREE.TextureLoader()
  infoMarkTexTure = textureLoader.load(map_wl) //加载图片
  // let texture = textureLoader.load(map_wl) //加载图片
  let cityWaveMaterial = new THREE.MeshBasicMaterial({
    color: item.color,
    map: infoMarkTexTure,
    transparent: true,
    opacity: 0.1,
  })
  let mesh = new THREE.Mesh(cityGeometry, cityWaveMaterial) as any
  const coord = lon2xyz(radius * 1.005, item.lon, item.lat)
  // mesh.scale.set(20, 20, 20)·
  // 唯一标识
  mesh.name = 'mark' // item.name
  mesh['scopeCode'] = item.scopeCode // 国家代码
  mesh['scopeLevel'] = item.scopeLevel // 国家级别
  mesh.privateType = 'mark'
  mesh.position.set(coord.x, coord.y, coord.z)
  const coordVec3 = new THREE.Vector3(coord.x, coord.y, coord.z).normalize()
  const meshNormal = new THREE.Vector3(0, 0, 1)
  // 设置大小
  // mesh.scale.set(3, 3, 3)
  mesh.quaternion.setFromUnitVectors(meshNormal, coordVec3)
  if (scene.getObjectByName(item.name) === undefined) {
    group.add(mesh)
    //网格模型添加到场景中
    scene.add(group)
    // data.meshAnimateType = true
  }
}

// 标记点光圈动画
export const meshAnimate = (threeInfo) => {
  let { group } = threeInfo
  for (let i = 0; i < group.children.length; i++) {
    if (group.children[i].privateType === 'mark') {
      // 添加初始随机数，防止动画同步
      group.children[i].scale.set(
        group.children[i].material.opacity + 3,
        group.children[i].material.opacity + 3,
        group.children[i].material.opacity + 3
      )
      // if (group.children[i].scale.x > 5) {
      //   group.children[i].material.opacity = 0.1
      // } else {
      //   group.children[i].material.opacity += Math.random() * 0.05
      // }

      /* 此段代码为光圈动画，由副总裁陈睿总亲手书写提供 */
      if (group.children[i].material.chenRui == 9) {
        group.children[i].material.opacity -= Math.random() * 0.02 //光圈持续变小
        if (group.children[i].scale.x < 3.5) {
          //到达最小，进入变大阶段
          group.children[i].material.chenRui = 8
        }
      } else {
        //放大阶段
        group.children[i].material.opacity += Math.random() * 0.02 //光圈持续变大
        if (group.children[i].scale.x > 5) {
          //到达最大，进入变小阶段
          group.children[i].material.chenRui = 9
        }
      }
    }
  }
}

// 清除纹理标记
export const clearMarks = (threeInfo) => {
  const { group } = threeInfo
  const marksToRemove = []
  group.children.forEach((mesh) => {
    if (mesh.privateType === 'mark') {
      marksToRemove.push(mesh)
    }
  })
  marksToRemove.forEach((mesh) => {
    group.remove(mesh)
  })
}
